package com.po;

import java.util.ArrayList;

/**
 * Created by zd on 2017/3/13 0013.
 */
public class ShopCart {
    private ArrayList<GoodsSingle> buylist = new ArrayList<GoodsSingle>();

    public ShopCart() {
    }

    public void addItem(GoodsSingle single){
        if(single!=null){
            if(buylist.size()==0){
                GoodsSingle temp = new GoodsSingle();
                temp.setName(single.getName());
                temp.setNum(single.getNum());
                temp.setPrice(single.getPrice());
                buylist.add(temp);
            }else {
                int i=0;
                for(;i<buylist.size();i++){
                    GoodsSingle temp = (GoodsSingle)buylist.get(i);
                    if (temp.getName().equals(single.getName())){
                        temp.setNum(temp.getNum()+1);
                        break;
                    }
                }
                if(i>=buylist.size()){
                    GoodsSingle temp = new GoodsSingle();
                    temp.setName(single.getName());
                    temp.setNum(single.getNum());
                    temp.setPrice(single.getPrice());
                    buylist.add(temp);
                }
            }
        }
    }

    public void removeItem(String name){

        for(int i=0;i<buylist.size();i++){
            GoodsSingle temp = (GoodsSingle)buylist.get(i);
            if (temp.getName().equals(name)){
                if(temp.getNum()>1){
                    temp.setNum(temp.getNum()-1);
                    break;
                }else if(temp.getNum()==1){
                    buylist.remove(i);
                }
            }
        }
    }
    public void clearCar(){
        buylist.clear();
    }

    public ArrayList<GoodsSingle> getBuylist() {
        return buylist;
    }

    public void setBuylist(ArrayList<GoodsSingle> buylist) {
        this.buylist = buylist;
    }
}
